package com.xuxin.nantai.model;

import com.xuxin.nantai.base.BaseConstant;
import com.xuxin.nantai.base.BaseObserver;
import com.xuxin.nantai.bean.BaseBean;
import com.xuxin.nantai.bean.ChaiBean;
import com.xuxin.nantai.contract.ChaiContract;
import com.xuxin.nantai.network.RetrofitApi;
import com.xuxin.nantai.network.Retrofits;
import com.xuxin.nantai.presenter.ChaiPresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Created by xu_xin
 * Created on 2020.06.19 0019
 */
public class ChaiModel implements ChaiContract.Model {
    @Override
    public void getChai(ChaiPresenter presenter, String LoginName, String ProductInfo, String InspectorInfo, int PageIndex) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .get_chai(LoginName, ProductInfo, InspectorInfo, PageIndex, BaseConstant.page_limit)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<ChaiBean>() {
            @Override
            public void onNext(ChaiBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }

    @Override
    public void delete(ChaiPresenter presenter, String UserName, String Guid) {
        Retrofits.getInstance().createReq(RetrofitApi.class)
                .delete_chai(UserName, Guid)
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<BaseBean>() {
            @Override
            public void onNext(BaseBean bean) {
                if (BaseConstant.success_code.equals(bean.getCode())) {
                    presenter.onSuccess(bean);
                } else {
                    presenter.onError(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                presenter.onError(null);
            }

            @Override
            public void onComplete() {
                presenter.onComplete();
            }
        });
    }
}